import { EntityManager, MikroORM } from '@mikro-orm/mysql';
import { InjectEntityManager, InjectMikroORM } from '@mikro-orm/nestjs';
import { Controller, Get } from '@nestjs/common';
import { knext } from 'knext';
import { TypechoContents } from '@entities/typecho/TypechoContents';
import { JsonApiResponse } from '@utils/json.api.response';
import * as Axios from 'axios'
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { TypechoFields } from '@databases/entities/typecho/TypechoFields';
import { TypechoMetas } from '@databases/entities/typecho/TypechoMetas';
import { hump2UnderLine, hump2UnderLineForArray, hump2UnderLineForObject, knexPrefix } from '@databases/vo/dao-helper';


@Controller('contents')
export class ContentsController {
    constructor(
        @InjectMikroORM('typecho')
        private readonly orm : MikroORM,
        @InjectEntityManager('typecho')
        private readonly em : EntityManager,
        private readonly httpService: HttpService
    ){}

    @Get('fetch')
    async fecth(){
        const knex = knext(this)

        var res : any = {data:{count:0}}
        var page = 60
        do {
            res = await firstValueFrom(this.httpService.post('https://api.ruletree.club/typechoContents/contentsList',
            {
                //searchParams: `{"type":"post","isswiper":1}`,
                //searchParams: `{"type":"post","istop":1}`,
                searchParams: `{"type":"post","istop":0}`,
                limit: 5,
                page: page++,
                order: 'modified'
            },{
                headers: {
                    "Content-Type":"application/x-www-form-urlencoded",
                }
            }))

            // const cids = res.data.data.map(post=>post.cid)
            // const ww = cids

            for (const data of res.data.data) {
                var result = await firstValueFrom(this.httpService.post('https://api.ruletree.club/typechoContents/contentsInfo',
                {
                    key:data.cid,
                    isMd:data.markdown
                },{
                    headers: {
                        "Content-Type":"application/x-www-form-urlencoded",
                    }
                }))

                var post = result.data

                const fields = hump2UnderLineForArray(post.fields)

                if(fields.length)
                {
                    await knex.fromORM(TypechoFields)
                    .insert(fields).onConflict().ignore()
                }

                var metas = post.category.map(category=>{
                    category.order = category.orderKey
                    delete category.orderKey
                    return category
                })

                post.order = post.orderKey

                delete post.fields //
                delete post.images //
                delete post.shop  //
                delete post.tag //
                delete post.authorInfo //
                delete post.category //
                delete post.markdown //
                delete post.orderKey //
                delete post.replyTime //

                // post = hump2UnderLineForObject(post)

                if(metas.length)
                {
                    await knex.fromORM(TypechoMetas)
                    .insert(metas).onConflict().merge()
                }

                await knex.fromORM(TypechoContents)
                .insert(post).onConflict().merge()

                console.log(post.title)
            }

            // const result  =await knex.fromORM(TypechoContents)
            // .createOrUpdate([],)
        } while (res.data.count);

        // const result  =await knex.fromORM(TypechoContents)
        // .createOrUpdate([],)
        // .where({
        //     "type":"post"
        // })

        return 'OK'
    }

    @Get('list')
    async list() {
        const knex = knext(this)

        const result  =await knex.fromORM(TypechoContents).where({
            "type":"post"
        })

        return JsonApiResponse.object(result)
    }
}
